<?php

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$setting=$this->plugin_setting('setting');
$tabs=array(
	array('op'=>'list','title'=>'团队长等级管理'),
	array('op'=>'edit','title'=>'添加/编辑等级'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
    $orderby='level asc';
	$total='';
	$return['list']=$this->getall('teamlevel',$params,array(),'',$orderby);
	foreach($return['list'] as &$item){
		$item['commissions']=$item['commissions']?json_decode($item['commissions'],true):array();
	}
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：几个能量值+几个积分兑换成1张兑换券（设置--商城设置--基础设置，里面设置兑换规则）：兑换券可以在商城抵扣兑换商品！
		</br>2、直推能量值收益：按直推购物赠送的积分数量，计算直推人获得百分之几的能量值；
		</br>3、间推能量值收益：按间推购物赠送的积分数量，计算间推人获得百分之几的能量值；
		</br>4、团队积分收益：包含我直推和我的无限级团队用积分参与拆盲盒游戏，那么就按照投入积分的数量乘以百分比等于团队积分收益；如果我的直推下级和我平级，我只能拿平一级积分收益；
		</br>5、级差积分收益：按级差获取积分；
		</br>6、平一级积分收益：平一级是拿我直推的无限级会员获得的【直推积分+团队积分+级差积分】的总和，按总和百分比计算我的平一级积分收益；
		</br>7、等级能量值奖励：设置这个等级能量值奖励的数量，只要达到这个等级就获得这个能量值奖励的数量；
		','edit'=>false,'list'=>array(
			array('name'=>'title','title'=>'等级名称','type'=>'string'),
			array('name'=>'level','title'=>'等级权重（数字越大，权重越大）','type'=>'string'),
			array('type'=>'string','name'=>'fee_per1','title'=>'直推能量值','help'=>'填写数字就是固定金额，填带%的就是按百分比；'),
			array('type'=>'string','name'=>'fee_per2','title'=>'间推能量值','help'=>'填写数字就是固定金额，填带%的就是按百分比；'),
			array('type'=>'string','name'=>'jifen_per1','title'=>'团队积分收益','help'=>'填写数字就是固定金额，填带%的就是按百分比；'),
			array('type'=>'string','name'=>'jifen_per2','title'=>'级差积分收益','help'=>'填写数字就是固定金额，填带%的就是按百分比；'),
			array('type'=>'string','name'=>'ping_per','title'=>'平一级积分收益','help'=>'填写数字就是固定金额，填带%的就是按百分比；'),
			array('type'=>'string','name'=>'level_per','title'=>'等级能量值奖励','help'=>'填写数字就是固定金额，填带%的就是按百分比；'),
		),'action'=>array(
			array('op'=>'look_condition','title'=>'升级条件','type'=>'modal','show'=>array('level >'=>0)),
			array('op'=>'edit','title'=>'编辑','type'=>'href','show'=>array('level >'=>0)),
		)),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='look_condition'){
	$item=$this->get('teamlevel',array('id'=>intval($_RQ['id']),'uniacid'=>$_SESSION['uniacid']),array('conditions'));
	if(!$item){
		exi('指定等级不存在！','error');
	}
	$item['conditions']=json_decode($item['conditions'],true);
	$return['item']=$item;
	if(!$item['conditions']){
		exi('该等级尚未设置升级条件！','error');
	}
	$htmls=array(
		'type'=>'look',
		'look'=>array(),
	);
	$level_up_types=$this->level_up_types;
	$current_level_up_types=$this->model()->team_level_up_types();
	foreach($current_level_up_types as $type){
		$htmls['look'][]=array('type'=>'string','name'=>'conditions.'.$type,'title'=>$level_up_types[$type]);
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=$this->get('teamlevel',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['conditions']=$item['conditions']?json_decode($item['conditions'],true):array();
		$item['setdata']=$item['setdata']?json_decode($item['setdata'],true):array('commission1'=>'0.00');
	}else{
		$item=array(
			'title'=>'',
			'conditions'=>array('order_price'=>'0'),
			'setdata'=>array('commission1'=>'0.00'),
		);
	}
   
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'分类设置',
		'edits'=>array(array('title'=>'基本信息','list'=>array(
			array('name'=>'title','title'=>'等级名称','type'=>'string'),
			array('name'=>'level','title'=>'等级权重','type'=>'string','help'=>'填写一百以内的数字，数字越大表示等级越高！不同等级的等级权重不得相等！'),
		))),
	);
	//升级条件表单
	$condition_edit=array(
		'title'=>'升级条件',
		'list'=>array(),
	);
	$level_up_types=$this->level_up_types;
	$current_level_up_types=$this->model()->team_level_up_types();
	$conditions=array();
	foreach($current_level_up_types as $type){
		if($type=='level_num'){
			$conditions['level_id']=$item['conditions']['level_id'];
		}
		$conditions[$type]=$item['conditions'][$type];
		$condition_edit['list'][]=array('type'=>'string','name'=>'conditions.'.$type,'title'=>$level_up_types[$type]);
	}
	$item['conditions']=$conditions;
	$htmls['edits'][]=$condition_edit;
	$commission_edit=array(
		'title'=>'团队收益',
		'list'=>array(
			array('type'=>'string','name'=>'fee_per1','title'=>'直推能量值','help'=>'填写数字就是固定金额，填带%的就是按百分比；'),
			array('type'=>'string','name'=>'fee_per2','title'=>'间推能量值','help'=>'填写数字就是固定金额，填带%的就是按百分比；'),
			array('type'=>'string','name'=>'jifen_per1','title'=>'团队积分收益','help'=>'填写数字就是固定金额，填带%的就是按百分比；'),
			array('type'=>'string','name'=>'jifen_per2','title'=>'级差积分收益','help'=>'填写数字就是固定金额，填带%的就是按百分比；'),
			array('type'=>'string','name'=>'ping_per','title'=>'平一级积分收益','help'=>'填写数字就是固定金额，填带%的就是按百分比；'),
			array('type'=>'string','name'=>'level_per','title'=>'等级能量值奖励','help'=>'填写数字，固定数量；'),
		),
	);
	$htmls['edits'][]=$commission_edit;
	$return['item']=$item;
	$return['level']=$this->pdo_getall('teamlevel',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		//'area_type'=>intval($postdata['area_type']),
		'level'=>intval($postdata['level']),
		'conditions'=>$postdata['conditions']?ijson_encode($postdata['conditions']):'',
		'fee_per1'=>trim($postdata['fee_per1']),
		'fee_per2'=>trim($postdata['fee_per2']),
		'jifen_per1'=>trim($postdata['jifen_per1']),
		'jifen_per2'=>trim($postdata['jifen_per2']),
		'ping_per'=>trim($postdata['ping_per']),
		'level_per'=>trim($postdata['level_per']),
	);
	if($data['level']<=0 || $data['level']>100){
		exi('请填写正确的等级权重值，1-100','error');
	}
	
	$id=intval($postdata['id']);
	if($id){
		$islevel=$this->getcolumn('teamlevel',array('uniacid'=>$_SESSION['uniacid'],'level'=>$postdata['level'],'id <>'=>$id),'id');
		if($islevel){
			exi('该权重已设置过，不能重复！','error');
		}
		$this->update('teamlevel',$data,array('id'=>$id));
		exi('设置成功！','','list');
	}else{
		$islevel=$this->getcolumn('teamlevel',array('uniacid'=>$_SESSION['uniacid'],'level'=>$postdata['level']),'id');
		if($islevel){
			exi('该权重已设置过，不能重复！','error');
		}
		$data['uniacid']=$_SESSION['uniacid'];
		$this->insert('teamlevel',$data);
		exi('添加成功！','','list');
	}
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		$this->delete('teamlevel',array('id'=>$id));	
	}
	if(!$id){
		exi('默认等级不能删除！','error');
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->delete('teamlevel',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>